import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "../store";

// 局部组件拦截 判断当前用户是否有权限访问指定页面 如果没有返回

function hasurl(url, next) {

  // 登陆后存储的路由权限和要访问的路径是否一致 一致可以访问 不一致强制跳转到登录页
  let result = store.getters.getUser.menus_url.some(item => item == url)
  console.log(result);
  if (result) {
    next()
  } else {
    next('/home')
  }
}


Vue.use(VueRouter)


//封装一个路由数据 以便于添加可以根据路由信息添加选项
export const routersMenu = [

  {
    path: '/menu',
    component: () => import('../views/menu/menu'),
    meta: { title: "菜单管理" },
    beforeEnter(to, from, next) {
      hasurl('/menu', next)
    }

  },
  {
    path: '/role',
    component: () => import('../views/role/role'),
    meta: { title: "角色管理" },
    beforeEnter(to, from, next) {
      hasurl('/role', next)
    }
  },
  {
    path: '/admin',
    component: () => import('../views/admin/admin'),
    meta: { title: "管理员管理" },
    beforeEnter(to, from, next) {
      hasurl('/admin', next)
    }
  },
  {
    path: '/category',
    component: () => import('../views/category/category'),
    meta: { title: "商品分类" },
    beforeEnter(to, from, next) {
      hasurl('/category', next)
    }
  },
  {
    path: '/specs',
    component: () => import('../views/specs/specs'),
    meta: { title: "商品规格" },
    beforeEnter(to, from, next) {
      hasurl('/specs', next)
    }
  },
  {
    path: '/manage',
    component: () => import('../views/manage/manage'),
    meta: { title: "商品管理" },
    beforeEnter(to, from, next) {
      hasurl('/manage', next)
    }
  },
  {
    path: '/member',
    component: () => import('../views/member/member'),
    meta: { title: "会员管理" },
    beforeEnter(to, from, next) {
      hasurl('/member', next)
    }
  },
  {
    path: '/banner',
    component: () => import('../views/banner/banner'),
    meta: { title: "轮播图管理" },
    beforeEnter(to, from, next) {
      hasurl('/banner', next)
    }
  },
  {
    path: '/seckill',
    component: () => import('../views/seckill/seckill'),
    meta: { title: "秒杀活动" },
    beforeEnter(to, from, next) {
      hasurl('/seckill', next)
    }
  },
  // {
  //   path: '/ceshiwa',
  //   component: () => import('../views/ceshiwa'),
  //   meta: { title: "测试版本" }
  //   ,
  //   beforeEnter(to, from, next) {
  //     hasurl('/ceshiwa', next)
  //   }
  // },

]


const routes = [
  {
    path: '/login',
    component: () => import('../pages/login')
  }, {
    path: '/index',
    component: () => import('../pages/index'),
    children: [
      {
        path: "/home",
        component: () => import("../views/home"),
        meta: { title: '首页' }
      },
      ...routersMenu,
      // 二级路由重定向
      {
        path: "",
        redirect: "/home"
      }
    ]
  }, {
    // 一级路由重定向
    path: "*",
    redirect: "/index"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局登录拦截 全局前置守卫
router.beforeEach((to, from, next) => {
  // 判断是否去登录页登录
  if (to.path == '/login') {
    next()
    return
  }

  // 判断如果已登录 放行操作
  if (store.getters.getUser) {
    next()
    return
  }

  // 以上都不是 强制跳转到登录页
  next('/login')
})

export default router
